<?php
/**
* phpCan - http://idc.anavallasuiza.com/
*
* phpCan is released under the GNU Affero GPL version 3
*
* More information at license.txt
*/

namespace files\images;

defined('ANS') or die();

class Imagick extends Image implements iLibs {
	protected $image;
	protected $info;


	/**
	 * public function load (string $image)
	 *
	 * Load an image
	 */
	public function load ($image) {
		$this->image = new Imagick();

		$this->image->readImage($image);

		$this->info = array(
			'file' => $image,
		);

		return $this;
	}

	/**
	 * public function unload (void)
	 *
	 * Destroy an image
	 */
	public function unload () {
		$this->image->destroy();

		return $this;
	}


	/**
	 * public function save (string $filename)
	 *
	 * Save the image to file
	 */
	public function save ($filename = '') {
		if (!$filename) {
			$this->image->writeImage();
		} else {
			$this->image->writeImage($filename);
		}

		return $this;
	}


	/**
	 * public function resize (int $width, [int $height], [bool $enlarge])
	 *
	 * Resize an image
	 */
	public function resize ($width, $height = 0, $enlarge = false) {
		$width = intval($width);
		$height = intval($height);

		if (!$enlarge && $this->enlarge($width, $height, $this->image->getImageWidth(), $this->image->getImageHeight())) {
			return $this;
		}

		$fit = ($width === 0 || $height === 0) ? false : true;

		$this->image->scaleImage($width, $height, $fit);

		return $this;
	}


	/**
	 * public function crop (int $width, int $height, [int $x], [int $y])
	 *
	 * Crop an image
	 */
	public function crop ($width, $height, $x = 0, $y = 0) {
		$x = $this->position($x, $width, $this->image->getImageWidth());
		$y = $this->position($y, $height, $this->image->getImageHeight());

		$this->image->cropImage($width, $height, $x, $y);

		return $this;
	}


	/**
	 * public function flip (void)
	 *
	 * Invert an image vertically
	 */
	public function flip () {
		$this->image->flipImage();

		return $this;
	}


	/**
	 * public function flop (void)
	 *
	 * Invert an image horizontally
	 */
	public function flop () {
		$this->image->flopImage();

		return $this;
	}


	/**
	 * public function zoomCrop (int $width, int $height)
	 *
	 * Crop an resize an image to specific dimmensions
	 */
	public function zoomCrop ($width, $height) {
		$width_resize = ($width/$this->image->getImageWidth()) * 100;
		$height_resize = ($height/$this->image->getImageHeight()) * 100;

		if ($width_resize < $height_resize) {
			$this->resize(0, $height);
		} else {
			$this->resize($width, 0);
		}

		$this->crop($width, $height, 'center', 'middle');

		return $this;
	}


	/**
	 * public function show ([bool $header])
	 *
	 * Return the image
	 */
	public function show ($header = true) {

		//Show header mime-type
		if ($header) {
			$format = strtolower($this->image->getImageFormat());
			$header = '';

			switch ($format) {
				case 'jpeg':
				case 'jpg':
				case 'gif':
				case 'png':
					$header = "image/$format";
					break;
			}

			if ($header) {
				header('Content-Type: '.$header);
			}
		}

		echo $this->image->getImageBlob();

		die();
	}


	/**
	 * public function rotate (int $degrees, [int $background])
	 *
	 * Rotate an image
	 */
	public function rotate ($degrees, $background = null) {
		if (is_null($background)) {
			$background = 'white';
		}

		if (preg_match('/^#?[0-9abcdef]{6}$/i', $background) && !strstr($background, '#')) {
			$background = '#'.$background;
		}

		$this->image->rotateImage($background, $degrees);

		return $this;
	}


	/**
	 * public function merge (string/object $image, [int $x], [int $y])
	 *
	 * Merge two images in one
	 */
	public function merge ($image, $x = 0, $y = 0) {
		if (is_object($image)) {
			$object_image = $image;
		} else {
			global $Config;

			$object_image = new Imagick();
			$object_image->readImage($Config->paths['web'].$image);
		}

		$x = $this->position($x, $object_image->getImageWidth(), $this->image->getImageWidth());
		$y = $this->position($y, $object_image->getImageHeight(), $this->image->getImageHeight());

		$this->image->compositeImage($object_image, $object_image->getImageCompose(), $x, $y);
		$this->image->flattenImages();

		return $this;
	}

	/**
	 * public function convert (string $format)
	 *
	 * Convert an image to another format
	 */
	public function convert ($format) {
		$this->image->setImageFormat($format);

		return $this;
	}
}
?>